package com.kotlin.zds.web

import android.webkit.JavascriptInterface
import android.webkit.WebView
import androidx.core.app.NotificationManagerCompat
import com.kotlin.zds.R
import com.zds.support.base.SupportBaseActivity
import com.zds.support.base.SupportBaseApplication
import com.zds.support.manager.UserDataManager
import com.zds.support.util.AppUtils
import com.zds.support.util.ScreenUtils
import com.zds.support.util.StatusBarUtils
import org.json.JSONException


class WebviewJavascriptInterface(private val activity: SupportBaseActivity,
                                 private val webView: WebView = WebView(activity),
                                 private val isEnableSetStatusBarLightMode: Boolean = true) {

    private val isNeedHandleBackPressedUrl = ArrayList<String>()
    var refreshKey: String = ""

    private var mJsCallBackInterface:JsCallBackInterface?=null

    /**
     * 回传一个json数据格式给app
     */
    @JavascriptInterface
    @Throws(JSONException::class)
    fun call(msg: String?) {
        mJsCallBackInterface?.jsCall(msg)
    }

    /**
     * 获取登录用户token
     */
    @JavascriptInterface
    fun getToken(): String {
        return UserDataManager.instance.getToken()
    }

    /**
     * 退出登录
     */
    @JavascriptInterface
    fun logout(code: String?, msg: String?) {
        activity.runOnUiThread {
            val app = activity.application
            if (app is SupportBaseApplication) {
                app.logout(code, msg)
            }
        }
    }

    /**
     * jsbridge 设置是否需要处理返回按钮事件
     * 需要处理返回按钮事件时，当用户按了返回按钮会调用js onBackPressed() 方法
     */
    @JavascriptInterface
    fun ysNeedHandleBackPressed(isNeedHandle: Boolean) {
        activity.runOnUiThread {
            try {
                val url = webView.url
                if (isNeedHandle) {
                    if (url != null) {
                        isNeedHandleBackPressedUrl.add(url)
                    }
                } else {
                    isNeedHandleBackPressedUrl.remove(url)
                }
            } catch (e: Exception) {

            }
        }

    }

    /**
     * 判断当前H5是否需要处理返回按钮事件
     */
    fun checkIsNeedHandleBackPressed(): Boolean {
        try {
            val url = webView.url
            return isNeedHandleBackPressedUrl.contains(url)
        } catch (e: Exception) {
            return false
        }
    }

    /**
     * 直接关闭当前webview页面
     */
    @JavascriptInterface
    fun finishActivity() {
        activity.runOnUiThread {
            try {
                activity.finish()
            } catch (e: Exception) {

            }
        }
    }


    @JavascriptInterface
    fun openShare(shareType: Int, title: String, content: String, contentImgUrl: String,
                  targetUrl: String,
                  remark: String) {
        //  runOnUiThread(Runnable { showShareDialog(shareType, title, content, contentImgUrl, targetUrl, remark) })
    }

    @JavascriptInterface
    fun getStatusBarHeight(): Int {
        return ScreenUtils.getStatusHeight(activity)
    }

    @JavascriptInterface
    fun getActionBarHeight(): Int {
        return activity.resources.getDimensionPixelSize(R.dimen.dp_48);
    }

    @JavascriptInterface
    fun getScreenWidth(): Int {
        return ScreenUtils.getScreenWidth(activity)
    }

    @JavascriptInterface
    fun getScreenHeight(): Int {
        return ScreenUtils.getScreenHeight(activity)
    }

    @JavascriptInterface
    fun setTitle(title: String) {
        activity.runOnUiThread {
            activity.setTitle(title)
        }

        // RxBus.send(WebViewEvent(7, title))
    }

    @JavascriptInterface
    fun setTitleTextColor(color: String) {
    }

    @JavascriptInterface
    fun setStatusBarLightMode(lightMode: Boolean) {
        if (isEnableSetStatusBarLightMode) {
            activity.runOnUiThread {
                if (lightMode) {
                    StatusBarUtils.setStatusBarLightMode(activity)
                } else {
                    StatusBarUtils.setStatusBarDarkMode(activity)
                }
            }
        }
    }

    @JavascriptInterface
    open fun setWebRefreshKey(key: String) {
        refreshKey = key
    }

    @JavascriptInterface
    open fun sendWebRefreshEvent(key: String) {
        try {
            activity.runOnUiThread {
            }
        } catch (e: Exception) {
        }
    }

    /**
     * 打开一个新的webview页面
     */
    @JavascriptInterface
    open fun ysOpenUrl(title: String, url: String) {
        try {
            activity.runOnUiThread {
                try {
                    WebViewActivity.startActivity(activity, url, title)
                } catch (e: Exception) {
                }
            }
        } catch (e: Exception) {

        }
    }

    /**
     * 打开一个新的webview页面
     */
    @JavascriptInterface
    open fun ysOpenUrl(title: String, url: String, canShare: Int) {
        try {
            activity.runOnUiThread {
                try {
                    WebViewActivity.startActivity(activity, url, title)
                } catch (e: Exception) {

                }
            }
        } catch (e: Exception) {
        }
    }

    @JavascriptInterface
    fun notification(): String {
        return NotificationManagerCompat.from(activity).areNotificationsEnabled().toString()
    }

    @JavascriptInterface
    fun getAppVersionCode(): String {
        return AppUtils.getVersionCode().toString()
    }

    @JavascriptInterface
    fun getAppVersionName(): String {
        return AppUtils.getVersionName()
    }

    @JavascriptInterface
    fun getAppDeviceType(): String {
        return "androidPhone"
    }
    fun setmJsCallBackInterface(mJsCallBackInterface: JsCallBackInterface?) {
        this.mJsCallBackInterface = mJsCallBackInterface
    }
}